package nix.anim.listener;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.common.json.JSON;

import nix.anim.mapper.LogLoginMapper;
import nix.anim.pojo.LogLogin;
import nix.anim.utils.HttpUtils;

/**
 * 登陆日志监听器：记录登陆日志
 */
public class LogLoginListener implements MessageListener {

	@Autowired
	private LogLoginMapper logLoginMapper;
	
	@Override
	public void onMessage(Message message) {
		try {
			if (message instanceof TextMessage) {
				// 从消息中获取LogLogin对象
				TextMessage textMessage = (TextMessage) message;
				String json = textMessage.getText();
				LogLogin logLogin = JSON.parse(json, LogLogin.class);
				logLogin.setLocation(HttpUtils.getAddresses(logLogin.getIp()));
				// 插入数据
				logLoginMapper.insert(logLogin);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
